import java.util.ArrayDeque;
import java.util.Deque;

/**
 * @author LKQ
 * @date 2022/1/6 9:32
 * @description 栈
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.simplifyPath("/a/./b/../../c/");
    }

    public String simplifyPath(String path) {
        String[] names = path.split("/");
        Deque<String> stack = new ArrayDeque<String>();
        for (String name : names) {
            if ("..".equals(name)) {
                if (!stack.isEmpty()) {
                    stack.pollLast();
                }
            } else if (name.length() > 0 && !".".equals(name)) {
                stack.offerLast(name);
            }
        }
        StringBuffer ans = new StringBuffer();
        if (stack.isEmpty()) {
            ans.append('/');
        } else {
            while (!stack.isEmpty()) {
                ans.append('/');
                ans.append(stack.pollFirst());
            }
        }
        return ans.toString();
    }
}

